package com.summit.exam.servlet;


import com.summit.exam.dbdao.*;
import com.summit.exam.entity.*;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class AnswerServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=utf-8");

        //获取时间字符串
       //String timer = req.getParameter("time");
        //int time = getSeconds(timer);

        //1、获取试题编号
        String paperId = (String) req.getSession().getAttribute("paperId");
        String title = (String) req.getSession().getAttribute("title");

        //2、获取答题答案
        QuestionDao questionService = new QuestionDao();
        ArrayList<QuestionBean> questionArrayList = questionService.selectQuestion(Integer.parseInt(paperId)); //查询所有试题

        //获取考生答案
        ArrayList<String> arrayList = getList(req, questionArrayList);

        //计算分数
        int score = 10;
        AnswerBean answer = new AnswerBean();
        answer.setOption(arrayList);
        answer.setPaperId(Integer.parseInt(paperId));
        answer.setStdId((String) req.getSession().getAttribute("ipt_name"));

        AnswerDao answerService = new AnswerDao();
        GradeDao gradeService = new GradeDao();
        GradeBean grade = gradeService.selectGrade();

        int scores = answerService.judgeScore(answer, grade.getScore());

        //插入成绩表
        ScoreInfoDao scoreService = new ScoreInfoDao();

        boolean flag = scoreService.insertScore((String) req.getSession().getAttribute("ipt_name"), Integer.parseInt(paperId), scores, title);

        //查看所有考试
        PaperDao paperService = new PaperDao();
        ArrayList<PaperBean> paperArrayList = paperService.selectPerpar();

        req.setAttribute("paperArrayList", paperArrayList);

        //4、比较剩余时间和是否和
        if (flag) {
            req.setAttribute("str", "true");
        } else {
            req.setAttribute("str", "false");
        }

        req.getRequestDispatcher("/ExamSystem/techweb/studentTest.jsp").forward(req,  resp);

    }

    public int getSeconds(String timer) {
        //截取字符串
        int seconds;
        String[] strings = timer.split(" ");

        for (int i = 0; i < strings.length; i++) {
            System.out.println(strings[i] + " " + i);
        }

        return 0;
    }

    public String getString(HttpServletRequest req, int id) {
        String result = "";

        String[] str = req.getParameterValues(Integer.toString(id));

        if (str == null){
            return "E";
        }

        for (int i = 0; i < str.length; i++) {
            result += str[i] + ((i + 1) == str.length ? "" : ":");

            if (result.equals("1")){
                result = "A";
            } else if(result.equals("2")) {
                result = "B";
            } else if(result.equals("3")) {
                result = "C";
            } else if(result.equals("4")) {
                result = "D";
            }
        }

        return result;
    }

    public ArrayList<String> getList(HttpServletRequest req, ArrayList<QuestionBean> questions) {
        ArrayList<String> arrayList = new ArrayList<String>();

        Iterator it = questions.iterator();

        while (it.hasNext()) {
            QuestionBean question = (QuestionBean) it.next();

            arrayList.add(getString(req, question.getQuestionId()));

        }

        return arrayList;
    }
}
